import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        Cnt b = new Cnt();
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                b.count();
            }
        });
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                b.count();
            }
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
        System.out.println(b.a);
    }
}
class Cnt{
    int a = 0;
    synchronized public void count(){//可以修饰 方法 和 静态方法
        a++;
    }
}
class Solution {
    TreeNode ans = null;
    int depth = 0;
    int dfs(TreeNode root, int dep){
        if(root == null){
            depth = Math.max(depth,dep);
            return dep;
        }
        int left = dfs(root.left, dep+1);
        int right = dfs(root.right, dep+1);
        if(left == right && left == depth){
            ans = root;
        }
        return Math.max(left,right);
    }
    public TreeNode lcaDeepestLeaves(TreeNode root) {
        dfs(root,0);
        return ans;
    }
}
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    private void dfs(int[] candidates, int target, int i, int sum, List<Integer> ret,boolean[] used){
        if(sum > target) return;
        if(sum == target){
            ans.add(new ArrayList<>(ret));
            return;
        }
        for(int j=i;j<candidates.length; j++){
            if(j > 0 && candidates[j-1] == candidates[j] && !used[j-1]){
                continue;
            }
            if(sum+candidates[j] <= target){
                ret.add(candidates[j]);
                used[j] = true;
                dfs(candidates,target,j+1,sum+candidates[j],ret,used);
                used[j] = false;
                ret.remove(ret.size()-1);
            }
        }
    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<Integer> ret = new ArrayList<>();
        Arrays.sort(candidates);
        boolean[] used = new boolean[candidates.length];
        dfs(candidates,target,0,0,ret,used);
        return ans;
    }
}

